﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Super.Common;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;
using HtmlAgilityPack;
using Super.Mvc;
namespace Super.Web.Areas.Admin.Controllers
{
    public class DingGouController : BaseController
    {
        #region 订购产品管理
       

        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var productService = new DGProductService(adp);
            var page = Query.Params("pageNum");
            PageSize = 9;
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
          
            var keyword = Query.Post("keyword");
            var state = Query.Post("pState").ToInt(-1);
            ViewBag.pState = state;
            var category = Query.Post("Category").ToInt(0);

            var where = PredicateBuilder.True<DGProduct>();
            where = where.And(a => a.IsDel == false);
            if(!string.IsNullOrEmpty(keyword))
            {
                var id = keyword.ToInt(0);
                where = where.And(a => a.ID == id||a.Title.Contains(keyword));
                ViewBag.keyword = keyword;
            }
            if(state>=0)
            {
                where = where.And(a => a.State == state);
            }
            if(category>0)
            {
                where = where.And(a => a.Category_ID == category);
            }

            var produts = productService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(1), PageSize);
            BindPage(produts.ItemCount, page.ToInt(), PageSize);
            ViewBag.Records = produts.ItemCount;
            getDingGouCategory(category);
            return View(produts.ItemCollection.ToList());
        }

        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt(0);
            GetCategory(null);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var productService = new DGProductService(adp);
                var productModel = productService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(productModel);
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Edit(DGProduct Model)
        {
            var Description = Query.Post("description", isEnableValidate: false);
            Model.Description = Description;
            var adp = new DbContextAdapter();
            var productService = new DGProductService(adp);
            productService.Repository.InsertOrUpdate(Model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "dinggouIndex");
        }

        //获取产品分类
        public void GetCategory(int? selectValue)
        {
            var adp = new DbContextAdapter();
            var categoryService = new ProductCategoryService(adp);
            var categoryList = categoryService.Repository.Find(a => a.IsDel == false).OrderBy(a=>a.Sort);
            ViewBag.Category = new SelectList(categoryList, "ID", "Name", selectValue);
        }

        //获取产品分类
        public void getDingGouCategory(int? selectValue)
        {
            var adp = new DbContextAdapter();
            var categoryService = new ProductCategoryService(adp);
            var categoryList = categoryService.getDingGouCategory();
            ViewBag.Category = new SelectList(categoryList, "ID", "Name", selectValue);
        }

        public JsonResult Delete()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var productService = new DGProductService(adp);
            var productModel = productService.Repository.Find(a => a.ID == id).FirstOrDefault();
            if (productModel != null)
            {
                productService.Repository.Delete(productModel,true);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "dinggouIndex");
        }

        #endregion

        #region 订购出单

        public ActionResult Search()
        {
            var adp = new DbContextAdapter();
            var productService = new DGProductService(adp);
            var page = Query.Params("pageNum");
            PageSize = 9;
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
            var keyword = Query.Post("keyword");

            var where = PredicateBuilder.True<DGProduct>();
            where = where.And(a => a.IsDel == false & a.State==1);

            if(!string.IsNullOrEmpty(keyword))
            {
                var id = keyword.ToInt(0);
                where = where.And(a => a.ID == id ||
                    a.Title.Contains(keyword));
                ViewBag.keyword = keyword;
            }

            var produts = productService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(1), PageSize);
            BindPage(produts.ItemCount, page.ToInt(), PageSize);
            ViewBag.Records = produts.ItemCount;
            return View(produts.ItemCollection.ToList());
        }

        public ActionResult Buy()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var dproductService = new DGProductService(adp);
            var dproductModel = dproductService.Repository.Find(a => a.ID == id).FirstOrDefault();
            return View(dproductModel);
        }

        [HttpPost]
        public JsonResult Buy(int?z)
        {
            var id = Query.Post("productId").ToInt(0);
            var name = Query.Post("ContactName");
            var phone = Query.Post("Phone");
            var dingjin = Query.Post("Dingjin");
            var remark = Query.Post("Remark");

            var adp = new DbContextAdapter();
            var dproductService = new DGProductService(adp);
            var dgproductModel = dproductService.Repository.Find(a => a.ID == id).FirstOrDefault();
            if(dgproductModel==null)
            {
                return JsonBack.Tip("实体为null");
            }
            var dgorderService = new DGOrderService(adp);

            var model=new DGOrder();
            model.Price = dgproductModel.LSPrice;
            model.Phone = phone;
            model.ContactName = name;
            model.DingJin = dingjin.ToDecimal(0);
            model.Remark = remark;
            model.CreateTime = DateTime.Now;
            model.DGProduct_ID = dgproductModel.ID;
            model.OrderNo = Number.GetSerialNumber(4).ToString();

            dgproductModel.Count += 1;
            dgproductModel.VirtualCount += 1;
            dproductService.Repository.InsertOrUpdate(dgproductModel);
            dgorderService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();

            return JsonBack.Tip(adp.ErrorMessage, "dgorderIndex");


        }

        #endregion


    }
}
